package curves;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;

import function.FunVariations;
import function.Function;
import function.Functions;
import function.SyntaxErrorException;

public class CurveMenuBar extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JMenuBar menuBar;
	
	public JMenuBar setmenu(){
		return this.menuBar;
		}
	
	public CurveMenuBar(final CurveFrame currentFrame){
		
		// Create the menu bar.
		this.menuBar = new JMenuBar();
		
		// Build the first menu.
		JMenu menu = new JMenu ("Fichier");
		menu.setMnemonic(KeyEvent.VK_F);
		menuBar.add(menu);

		//	a group of JMenuItems
		JMenuItem nouvelleFenetre, fermer;
		
		nouvelleFenetre = new JMenuItem("Nouvelle fenetre");
		nouvelleFenetre.setMnemonic(KeyEvent.VK_N);
		nouvelleFenetre.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				final JDialog d = new JDialog();
				JPanel pane = new JPanel();
				JLabel fonction = new JLabel("Fonction :");
				JButton bouton = new JButton("Ok");
				final JTextField text = new JTextField(20);
				d.setContentPane(pane);
				d.setMinimumSize(new Dimension(300, 100));
				d.setLocationRelativeTo(currentFrame);
				d.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				pane.add(fonction);
				pane.add(text);
				pane.add(bouton);
				d.setVisible(true);
			
				ActionListener fonctionListener = new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						try{
							Function fun = Functions.parse(text.getText());
							FunctionVariations fvar = (FunctionVariations)new FunVariations(fun, -2 * Math.PI, 2 * Math.PI);
							fvar.tabulate(1);
							new CurveFrame(fvar);
							d.dispose();
						} catch (SyntaxErrorException e1){
							text.setText("Fonction invalide !");
							text.selectAll();
						} catch (IOException e1){
				 			e1.getStackTrace();
						}
					}					
				};
				bouton.addActionListener(fonctionListener);
				text.addActionListener(fonctionListener);
			}
		});

		fermer = new JMenuItem("Fermer");
		fermer.setMnemonic(KeyEvent.VK_F);
		fermer.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				final JDialog d = new JDialog();
				JPanel pane = new JPanel();
				JLabel quitter = new JLabel("Etes-vous sur de vouloir quitter ?");
				JButton oui = new JButton("Oui");
				JButton non = new JButton("Non");
				d.setContentPane(pane);
				d.setMinimumSize(new Dimension(250, 100));
				d.setLocationRelativeTo(currentFrame);
				d.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
				pane.add(quitter);
				pane.add(oui);
				pane.add(non);
				d.setVisible(true);
			
				oui.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						currentFrame.dispose();
						d.dispose();
					}					
				});
			
				non.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e) {
						d.dispose();
					}					
				});
			}
		});
	
		menu.add(nouvelleFenetre);
		menu.add(fermer);
	}
}
